package org.example.service.serviceImpl;


import org.example.exception.ExamException;
import org.example.model.Enum.ExamPermission;
import org.example.model.PoJo.Student;
import org.example.model.StudentAccount;
import org.example.service.StudentLogin;

public class StudentLoginImpl implements StudentLogin {
    @Override
    public boolean login(String username, String password) {
        StudentAccount.init();
        int usernameCount = 0;
        int passwordCount = 0;
        for (Student student : StudentAccount.studentList) {
            if (username.equals(student.getUsername())) {
                usernameCount++;
                if (password.equals(student.getPassword())) {
                    passwordCount++;
                    if (student.getPermission().equals(ExamPermission.NULL_PERMISSION)) {
                        throw new ExamException("无考试权限");
                    }
                }
            }
        }

        if (usernameCount == 0 && passwordCount == 0) {
            throw new ExamException("账号不存在");
        } else if (passwordCount == 0) {
            throw new ExamException("密码错误");
        }
        return true;
    }
}
